package com.open.api.schedule;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.open.api.bo.MessageBO;
import com.open.api.config.property.FileProperty;
import com.open.api.config.property.FromSqlProperty;
import com.open.api.config.property.FtpProperty;
import com.open.api.ftp.common.ResponseData;
import com.open.api.ftp.common.TypeEnum;
import com.open.api.ftp.util.FileIOUtil;
import com.open.api.ftp.util.FtpUtil;
import com.open.api.mapper.SendinfoDao;
import com.open.api.model.ResultModel;
import com.open.api.pojo.Sendinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
//@Async
public class AlarmTask {

    @Resource
    private SendinfoDao sendinfoDao;

    @Autowired
    private FileProperty fileProperty;

    @Autowired
    private FtpProperty ftpProperty;

    @Autowired
    private FromSqlProperty fromSqlProperty;

    @Scheduled(cron = "0/30 * * * * *")
    public void run() throws InterruptedException {
        if(fromSqlProperty.getSfkqdsrw()){
            System.out.println(Thread.currentThread().getName()+"=====>>>>>使用cron  {}"+(System.currentTimeMillis()/1000));
            List<Sendinfo> sendinfoList= sendinfoDao.queryAll("N");
            if(sendinfoList!=null && sendinfoList.size()>0){
                for(int i=0;i<sendinfoList.size();i++){
                    Sendinfo sendinfo=sendinfoList.get(i);

                    JSONObject jsonObject=JSON.parseObject(sendinfo.getSendinfo());
                    MessageBO messageBO=JSON.toJavaObject(jsonObject,MessageBO.class);
                    ResultModel resultModel=new ResultModel();
                    FileIOUtil fileIOUtil=new FileIOUtil();
                    ResultModel resultModel_saveFile=fileIOUtil.saveDataToFile(messageBO,fileProperty.getUploaddir(),fileProperty.getSuffix());
                    String newFileName="";

                    if(resultModel_saveFile.isSuccess()){
                        newFileName= (String) resultModel_saveFile.getData();
                        FtpUtil ftpUtil=new FtpUtil();
                        String localTmpSendPath=fileProperty.getUploaddir()+messageBO.getSystem()+"/"+newFileName;
                        ResponseData responseData =ftpUtil.defaultUpload(ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getSendpath(),newFileName,new File(localTmpSendPath));
                        System.out.println(responseData.toString());
                        if(responseData.getCode()==0){
                            System.out.println("上传ftp成功");
                            ResponseData responseData_delFile=fileIOUtil.deleteFromServer(localTmpSendPath, TypeEnum.DELETETYPE_RMFILE.getCode());
                            if(responseData_delFile.getCode()==0){
                                resultModel.setSuccess(true);
                                Sendinfo sendinfo1=new Sendinfo();
                                sendinfo1.setId(sendinfo.getId());
                                sendinfo1.setStatus("Y");
                                sendinfo1.setReadtime(new Date());
                                int s =sendinfoDao.updateByPrimaryKeySelective(sendinfo1);
                                if(s>0){
                                    System.out.println("数据推送成功!");
                                }else{
                                    System.out.println("数据推送失败!");
                                }
                            }else{
                                resultModel.setSuccess(false);
                                resultModel.setErrorCode(String.valueOf(responseData_delFile.getCode()));
                                resultModel.setErrorMsg(resultModel_saveFile.getErrorMsg());
                                System.out.println(resultModel);
                            }
                        }else {
                            resultModel.setSuccess(false);
                            resultModel.setErrorCode(String.valueOf(responseData.getCode()));
                            resultModel.setErrorMsg(responseData.getMsg());
                            System.out.println(resultModel);
                        }
                    }else{
                        System.out.println(resultModel_saveFile.toString());
                    }

                }
            }
        }
    }

}
